﻿using NUnit.Framework;
using System.IO;

namespace CsvImporter.Test
{
  [TestFixture]
  public class TestCsvImporter
  {
    [Test]
    public void OneRowOneCol()
    {
      var result = CsvImporter.Import("TestCsvImporterOneRowOneCol.csv");
      var expected = new[]
                       {
                         new[] {"a"},
                       };

      Assert.That(result, Is.EqualTo(expected));
    }

    [Test]
    public void OneRowTwoCols()
    {
      var result = CsvImporter.Import("TestCsvImporterOneRowTwoCols.csv");
      var expected = new[]
                       {
                         new[] {"1", "22"},
                       };

      Assert.That(result, Is.EqualTo(expected));
    }
    [Test]
    public void TwoRowsTwoCols()
    {
      var result = CsvImporter.Import("TestCsvImporterTwoRowsTwoCols.csv");
      var expected = new[]
                       {
                         new[] {"11111", "222"},
                         new[] {"333333", "ß4444444"},
                       };

      Assert.That(result, Is.EqualTo(expected));
    }

    [Test]
    public void EmptyFile()
    {
      var result = CsvImporter.Import("TestCsvImporterEmpty.csv");
      var expected = new string[0,0];

      Assert.That(result, Is.EqualTo(expected));
    }

    [Test, ExpectedException(typeof(FileNotFoundException))]
    public void FileNotExistingThrowsException()
    {
      CsvImporter.Import("FileNotExisting.csv");
    }

    [Test]
    public void CommaInsteadOfSemicolonWillBeIgnored()
    {
      var result = CsvImporter.Import("TestCsvImporterCommaInsteadOfSemicolonWillBeIgnored.csv");
      var expected = new[]
                       {
                         new[] {"aaaa;fgghj", "ghddklj"},
                         new[] {"klklkj"    , ";jkjlkj"},
                       };

      Assert.That(result, Is.EqualTo(expected));
    }
    [Test]
    public void TestCsvImporterThreeRowsDifferentNumberOfCols()
    {
      var result = CsvImporter.Import("TestCsvImporterThreeRowsDifferentNumberOfCols.csv");
      var expected = new[]
                       {
                         new[] {"11111", "222"},
                         new[] {"3"},
                         new[] {"44", "55555555", "6666666"},
                       };

      Assert.That(result, Is.EqualTo(expected));
    }

    [Test]
    public void TestCsvImporterEmptyRows()
    {
      var result = CsvImporter.Import("TestCsvImporterEmptyRows.csv");
      var expected = new[]
                       {
                         new[] {"aa", "bb"},
                         new[] {""},
                         new[] {"cccc"},
                         new[] {""},
                         new[] {""},
                         new[] {"ddddd", "eeeee", "ffffff"},
                       };

      Assert.That(result, Is.EqualTo(expected));
    }
  }
}
